package com.ruoyi.web.controller.wisdom;

import java.util.List;

import com.ruoyi.common.config.HeadNum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.GetSessionValueUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.assets.FzBusinessFloor;
import com.ruoyi.system.service.assets.IFzBuildingRoomService;
import com.ruoyi.system.service.assets.IFzBuildingUnitService;
import com.ruoyi.system.service.assets.IFzBusinessFloorService;
import com.ruoyi.wisdom.domain.FzServiceAdvertisement;
import com.ruoyi.wisdom.service.IFzServiceAdvertisementService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpSession;

/**
 * 发布广告Controller
 * 
 * @author ruoyi
 * @date 2020-03-05
 */
@Controller
@RequestMapping("/wisdom/advertisement")
public class FzServiceAdvertisementController extends BaseController
{
    private String prefix = "wisdom/advertisement";
    @Autowired
    private IFzBusinessFloorService fzBusinessFloorService;
    @Autowired
    private IFzBuildingUnitService fzBuildingUnitService;
    @Autowired
    private IFzServiceAdvertisementService fzServiceAdvertisementService;

    @RequiresPermissions("wisdom:advertisement:view")
    @GetMapping()
    public String advertisement()
    {
        return prefix + "/advertisement";
    }

    /**
     * 查询发布广告列表
     */
    @RequiresPermissions("wisdom:advertisement:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpSession session,FzServiceAdvertisement fzServiceAdvertisement)
    {
        startPage();
        fzServiceAdvertisement.setcId(GetSessionValueUtil.getCommunity());
        List<FzServiceAdvertisement> list = fzServiceAdvertisementService.selectFzServiceAdvertisementList(fzServiceAdvertisement);
        return getDataTable(list);
    }

    /**
     * 导出发布广告列表
     */
    @RequiresPermissions("wisdom:advertisement:export")
    @Log(title = "发布广告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FzServiceAdvertisement fzServiceAdvertisement)
    {
        List<FzServiceAdvertisement> list = fzServiceAdvertisementService.selectFzServiceAdvertisementList(fzServiceAdvertisement);
        ExcelUtil<FzServiceAdvertisement> util = new ExcelUtil<FzServiceAdvertisement>(FzServiceAdvertisement.class);
        return util.exportExcel(list, "advertisement");
    }

    /**
     * 新增发布广告
     */
    @GetMapping("/add")
    public String add(ModelMap map,HttpSession session)
    {
        FzBusinessFloor sd=new FzBusinessFloor();
        sd.setcId(GetSessionValueUtil.getCommunity());
        List<FzBusinessFloor> floo = fzBusinessFloorService.selectFzBusinessFloorList(sd);
        map.put("floorSelect",floo);
        return prefix + "/add";
    }

    /**
     * 新增保存发布广告
     */
    @RequiresPermissions("wisdom:advertisement:add")
    @Log(title = "发布广告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FzServiceAdvertisement fzServiceAdvertisement)
    {
        fzServiceAdvertisement.setAdId(HeadNum.AD_HEAD+ DateUtils.getTimeMillis());
        fzServiceAdvertisement.setcId(GetSessionValueUtil.getCommunity());
        fzServiceAdvertisement.setCreateName(ShiroUtils.getSysUser().getUserName());
        return toAjax(fzServiceAdvertisementService.insertFzServiceAdvertisement(fzServiceAdvertisement));
    }

    /**
     * 修改发布广告
     */
    @GetMapping("/edit/{adId}")
    public String edit(ModelMap map,HttpSession session,@PathVariable("adId") String adId, ModelMap mmap)
    {
        FzServiceAdvertisement fzServiceAdvertisement = fzServiceAdvertisementService.selectFzServiceAdvertisementById(adId);
        mmap.put("fzServiceAdvertisement", fzServiceAdvertisement);
        FzBusinessFloor sd=new FzBusinessFloor();
        sd.setcId(GetSessionValueUtil.getCommunity());
        List<FzBusinessFloor> floo = fzBusinessFloorService.selectFzBusinessFloorList(sd);
        map.put("floorSelect",floo);
        return prefix + "/edit";
    }

    /**
     * 修改保存发布广告
     */
    @RequiresPermissions("wisdom:advertisement:edit")
    @Log(title = "发布广告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FzServiceAdvertisement fzServiceAdvertisement)
    {  fzServiceAdvertisement.setUpdateName(ShiroUtils.getSysUser().getUserName());
        return toAjax(fzServiceAdvertisementService.updateFzServiceAdvertisement(fzServiceAdvertisement));
    }

    /**
     * 删除发布广告
     */
    @RequiresPermissions("wisdom:advertisement:remove")
    @Log(title = "发布广告", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fzServiceAdvertisementService.deleteFzServiceAdvertisementByIds(ids));
    }
}
